package lidongp.dao;


import lidongp.entity.BjEntity;
import lidongp.entity.Page;
import lidongp.entity.lidongpengEntity;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Component;


import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
public class lidongpDao extends HibernateDaoSupport  {

    @Transactional
    public void save(lidongpengEntity lidongpEntity) {
        Session session= getSessionFactory().openSession();
        session.saveOrUpdate(lidongpEntity);
        session.flush();
    }




    public int queryCount(String name) {
        String sql = "select * from user where name like ? ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setParameter(0,  "%" + name + "%");
        List<lidongpengEntity> list=  query.list();
        return  list.size();
    }

    public List<lidongpengEntity> queryPage(Page page, String name) {
        String sql = "select a.name,a.id,a.url,b.bname from user a,clazz b where a.bid=b.id and  name like ? order by systime desc limit ?,? ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setParameter(0,  "%" + name + "%");
        query.setParameter(1,  page.getBeginIndex());
        query.setParameter(2,  page.getEveryPage());
        query.setResultTransformer(Transformers.aliasToBean(lidongpengEntity.class));
        List<lidongpengEntity> list=  query.list();
        return  list;
    }

    public  lidongpengEntity edit(lidongpengEntity lidongpEntity) {
        Session session= getSessionFactory().openSession();
        return (lidongpengEntity)session.get(lidongpengEntity.class,lidongpEntity.getId());
    }

    public void del(lidongpengEntity lidongpEntity) {
        Session session= getSessionFactory().openSession();
        session.delete(lidongpEntity);
        session.flush();
    }

    public List<BjEntity> bj() {
        String sql = "select * from clazz  ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setResultTransformer(Transformers.aliasToBean(BjEntity.class));
        List<BjEntity> list=  query.list();
        return  list;
    }



}
